package zust.javaee.service.Impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zust.javaee.dao.hibernate.BaseDao;
import zust.javaee.dto.corporation.CorporIndexNumDto;
import zust.javaee.entity.Tcorporation;
import zust.javaee.entity.Tmember;
import zust.javaee.entity.TrequisitionForm;
import zust.javaee.enums.FormStatusEnum;
import zust.javaee.service.CorporationService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liy on 2017/1/4.
 */
@Service
@Transactional
public class CorporationServiceImpl extends BaseServiceImpl<Tcorporation> implements CorporationService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private BaseDao<Tcorporation> corporationDao;

    @Autowired
    private BaseDao<Tmember> memberDao;

    @Autowired
    private BaseDao<TrequisitionForm> reqFormDao;


    @Override
    public List<CorporIndexNumDto> getAllCorporationDto(Integer playerId,Integer num, Short status) {
        return transToCorporIndexDtos(playerId, getAllCorporation(num, status));
    }

    @Override
    public boolean checkMemberExit(Integer playerId, Integer corporId) {
        String hql = "select count(*) from Tmember l where l.tcorporation.id = "
                + corporId + " and l.tplayer.id = " + playerId;
        return memberDao.count(hql) >= 1;
    }

    @Override
    public boolean checkOnRequestion(Integer playerId, Integer corporId) {
        String hql = "select count(*) from TrequisitionForm l where l.tcorporation.id = "
                + corporId + " and l.tplayer.id = " + playerId + " and l.status = "
                + FormStatusEnum.CHECKING.getStatus();
        return memberDao.count(hql) >= 1;
    }

    @Override
    public CorporIndexNumDto transToCorporIndexDto(Tcorporation tcorporation) {
        CorporIndexNumDto corpor = new CorporIndexNumDto();
        BeanUtils.copyProperties(tcorporation, corpor);
        return corpor;
    }

    @Override
    public List<CorporIndexNumDto> transToCorporIndexDtos(List<Tcorporation> tcorporations) {
        List<CorporIndexNumDto> cors = new ArrayList<>();
        for (Tcorporation cor : tcorporations)
            cors.add(transToCorporIndexDto(cor));
        return cors;
    }

    @Override
    public List<CorporIndexNumDto> transToCorporIndexDtos(Integer playerId, List<Tcorporation> tcorporations) {
        List<CorporIndexNumDto> cors = new ArrayList<>();
        for (Tcorporation cor : tcorporations){
            CorporIndexNumDto corDto = transToCorporIndexDto(cor);
            if (checkMemberExit(playerId, cor.getId()))
                corDto.setJoined(2);
            else if (checkOnRequestion(playerId, cor.getId()))
                corDto.setJoined(1);
            else
                corDto.setJoined(0);
            cors.add(corDto);
        }
        return cors;
    }

    @Override
    public List<Tcorporation> getAllCorporation() {
        String hql = "from Tcorporation";
        return corporationDao.find(hql);
    }

    @Override
    public List<Tcorporation> getAllCorporation(Short status) {
        String hql = "from Tcorporation c where c.status = " + status;
        return corporationDao.find(hql);
    }

    @Override
    public List<Tcorporation> getAllCorporation(Integer num, Short status) {
        String hql = "from Tcorporation c where c.status = " + status;
        return corporationDao.find(hql, num, 20);
    }
}
